home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_3.lha / 6_3 / 6_3c1.c < prev    next >
C/C++ Source or Header  |  1993-08-08  |  689b  |  33 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Assign something to a substring, version 1
  6. / Only allow equal length assignments.
  7. include <string.h>
  8. include <string.h>
  9.  
  10. / substring = char*
  11. ubstring& substring::operator=(char *cp)
  12.  
  13.    if (len != strlen(cp))
  14. error("lengths must match for assignment");
  15.  
  16.    // disconnect string
  17.    if (str->p->n > 1)
  18. {
  19. str->p->n--;
  20. struct srep *oldp = str->p;
  21. str->p = new struct srep;
  22. str->p->s = new char[strlen(oldp->s) + 1];
  23. strcpy(str->p->s, oldp->s);
  24. str->p->n = 1;
  25. s = str->p->s + (s - oldp->s);
  26. }
  27.  
  28.    if (len > 0)
  29. memcpy(s, cp, len);
  30.  
  31.    return *this;
  32.  
  33.